library(stringr)
library(ggplot2)
setwd('/home/song/project/going/neo4j/demo/logs');
files <- list.files()
#head(files)


extraVarFromFile = function(fileName,dbName){
  content = readChar(fileName, file.info(fileName)$size);
  patternLines = c(
    "(\\d+) INFO\\[main\\] \\[Time Usage\\] Total:(\\d+)ms, count:(\\d+), Average time:(\\d+\\.\\d+)ms\r\n"
  );
  patterns = paste(patternLines,collapse="");
  result = str_match_all(content, patterns);
  
  T.tmp0 = data.frame(result)
  T.tmp1 = data.frame(T.tmp0[-46,-1], stringsAsFactors = FALSE)
  #T.tmp1 = data.frame(T.tmp0[-1,-1], stringsAsFactors = FALSE)
  T.index = sapply(T.tmp1, is.factor)
  T.tmp1[T.index] = lapply(T.tmp1[T.index], function(x) as.numeric(as.character(x)))
  for(i in c(2:45)){
    T.tmp1[i,'X4']=T.tmp1[i-1,'X4']+T.tmp1[i,'X4']
  }
  T.tmp1["db"]=dbName
  T.tmp1
}


fileName="20160302-neo4jtemporal-get-range - 副本 - 副本.log";
T.tmp1 = extraVarFromFile(fileName,"TGraph")
T.tmp1 = T.tmp1[seq(1,45,3),]

fileName="20120302-neo4jArraySimulation-get-range.log";
T.tmp2 = extraVarFromFile(fileName,"Neo4j")
T.tmp2['X2']=T.tmp2['X2']-21920;
T.tmp2 = T.tmp2[seq(1,45,3),]

#T.tmp = rbind(T.tmp1,T.tmp2)
T.tmp = rbind(T.tmp2,T.tmp1)

# x:time,y:tx commited
P.3 = ggplot(T.tmp, aes(x=X2/1000,y=X4,colour=db,shape=db)) + geom_line() + geom_point(size=3)+
  theme_bw()+ theme(legend.justification=c(1,0), legend.position=c(1, 0),
                    legend.title=element_blank(),
                    legend.key=element_blank()
                    #axis.title=element_text(size=14)) + #,face="bold"
  )+
  scale_x_continuous(labels=comma) + 
  scale_y_continuous(labels=f2si) +
  #scale_y_continuous(trans='log') +
  labs(#title="Compare: committed transaction count",
    x="Time(s)",y="Number of transactions",fill="")

# 
# # x:time,y:GB write
# ggplot(T.tmp, aes(x=X2/1000,y=X4,colour=db)) + geom_line() + geom_point()+
#   scale_x_continuous(limits=c(0,81500)) + #scale_y_continuous(trans='log') +
#   labs(title="Compare: temporal data write to database",
#        x="time (s)",y="temporal data write to database (GB)");
# 
# # x:time,y:write speed
# ggplot(T.tmp, aes(x=X2/1000,y=X11,colour=db)) + geom_line() + geom_point()+
#   scale_x_continuous(limits=c(0,81500)) + #scale_y_continuous(trans='log') +
#   labs(title="Compare: average write speed of two database",
#        x="time (s)",y="speed (every 100 transactions) (KB/s)")

rm(list=setdiff(ls(), c("P.4","P.1","P.2","P.3")))